<!--
 * @Date: 2025-01-08 11:23:59
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2025-01-09 11:21:03
 * @Description: 表单预览的抽屉
-->
<template>
  <el-drawer
    v-if="drawerState.visible"
    v-model="drawerState.visible"
    :="drawerAttrs"
  >
    <div class="fp-preview-drawer-wrapper">
      <div class="preview-drawer-head">
        <div class="header-title">
          {{ renderState.baseInfo.name }}
        </div>
        <div class="header-steps"></div>
        <div class="header-tools">
          <el-button type="danger" @click="handleClose" >关闭</el-button>
        </div>
      </div>
      <div class="preview-drawer-body">
        <FpRender
          :="renderAttrs"
          @loaded="renderLoaded"
        />
      </div>
    </div>
  </el-drawer>
</template>

<script setup>
import { FpRender, FormPojo } from '@fp'
import { ElLoading } from 'element-plus';
import { reactive } from 'vue';

const drawerAttrs = reactive({
  size: '100%',
  class: 'fp-preview-drawer',
  zIndex: 2000,
  showClose: false,
  withHeader: false,
  appendToBody: true,
  closeOnPressEscape: false,
})
const drawerState = reactive({
  visible: false,
  loadService: null,
})

const handleOpen = (info) => {
  console.log('handleOpen', info)
  renderAttrs.formCode = info.code
  drawerState.visible = true
  drawerState.loadService = ElLoading.service({ fullscreen: true, text: '加载中......' })
  
}
const handleClose = () => {
  renderAttrs.formCode = ''
  renderState.baseInfo = JSON.parse(JSON.stringify(FormPojo.baseInfo))
  drawerState.visible = false
}

const renderAttrs = reactive({
  preview: true,
  disabled: true,
  formType: 'view',
  formCode: '',
  formData: { proId: 'preview' }
})
const renderState = reactive({
  baseInfo: JSON.parse(JSON.stringify(FormPojo.baseInfo)),
})
const renderLoaded = (formInfo) => {
  renderState.baseInfo = formInfo.baseInfo
  if (drawerState.loadService) {
    drawerState.loadService.close()
  }
}

defineExpose({
  handleOpen
})

</script>

<style lang="scss" scoped>
.fp-preview-drawer {
  &-wrapper {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
}
.preview-drawer-head {
  display: flex;
  width: 100%;
  padding: 0 8px;
  height: 50px;
  line-height: 50px;
  align-items: center;
  border-bottom: 1px solid #d9d9d9;
  background-color: #fff;
  .header-title {
    width: 300px;
    font-size: 16px;
  }
  .header-steps {
    flex: 1;
    line-height: 1;
    padding: 15px 120px 15px 30px;
  }
  .header-tools {
    text-align: right;
  }
}
.preview-drawer-body {
  flex: 1;
  overflow: auto;
  min-height: 100px;
}
</style>